<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        session('[destroy]');
        session('[regenerate]');
        $this->assign('actbnt', 'weui-tab__bd-item--active');
        $this->assign('tabbnt', 'weui-bar__item--on');
        $this->display('index');
    }

    public function bm(){
        $this->display('bm');
    }

    public function getcates(){

        $pid = 1;
        $datalist = array();
        $model = M("Categorys");
        $where['pid'] = 1;
        $result = $model->where($where)->field('id,name,remark')->select();
        foreach ($result as $k => $v){
            $newa = array();
           // $this->p($v);
            $newa['title'] = $v['name'];
            $newa['value'] = $v['id'];
            $newa['remark'] = $v['remark'];
            array_push($datalist, $newa);

        }
        $this->ajaxReturn($datalist,'json');


    }

    public function getinfo(){
       // $this->p($_SERVER['REQUEST_METHOD']);
        //$this->p($_POST);
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $showmodel = M('Shows');
            $showmodel->startTrans();
            $showdata['showname'] = trim(I('showname'));
            $showdata['contname'] = trim(I('contname'));
            $showdata['contid'] = trim(I('contid'));
            $showdata['contphone'] = trim(I('contphone'));
            $showdata['cat1'] = trim(I('cat1'));
            $showdata['cat2'] = trim(I('cat2'));
            $showdata['nums'] = trim(I('nums'));
            $showdata['showseq'] = '';
            $showdata['money'] = $this->getmoney($showdata['nums']);
            $showdata['salemoney'] = 50;
            $showdata['totalmoney'] = $this->getmoney($showdata['nums'])*$showdata['nums'];
            $showdata['paymoney'] = $showdata['totalmoney'] - $showdata['salemoney'];
            $showdata['indate'] = date("Y-m-d H:i:s",time());
            $showmodel->create($showdata);
            $id = $showmodel->add();
            $showdata['showseq']= $id.''.$showdata['contphone'];
            $showmodel->where('id='.$id)->save($showdata);
            session('showseq',$showdata['showseq']);
            session('pnum', $showdata['nums']);
            session('money', $this->getmoney($showdata['nums']));
            session('showname', $showdata['showname']);
            session('paymoney', $showdata['paymoney']);
            session('salemoney', $showdata['salemoney']);
            session('totalmoney', $this->getmoney($showdata['nums'])*$showdata['nums']);
            $showpmodel = M('Showperson');
            $persons = I('p');


            foreach ($persons as $k=>$v){
                $showperson['showid'] = $id;
                $showperson['pname'] = trim($v['pname']);
                $showperson['pid'] = trim($v[pid]);
                $showperson['indate'] = date("Y-m-d H:i:s",time());
                $showpmodel->create($showperson);
                $presult = $showpmodel->add();
                // $this->p($presult);
                if($presult <=0){
                    $this->p('插入参赛人员信息失败');
                }
            }
            $showmodel->commit();
            //$showmodel->commit();
            //$this->p($showdata);
            redirect('ok',1,'报名中，请稍等...');

            //$this->display('ok');
        }else{
            $this->display('index');
        }

    }

    public function queryshow(){
        $contid = I('contid');
        $model = M('Shows');
        $where['contid'] = $contid;
        $result = $model->where($where)->select();

        $pmodel = M('Showperson');
        $where['showid'] = $result[0]['id'];
        $presult = $pmodel->where($where)->select();
       // $this->p($presult);die;
        $this->assign('actbnt1', 'weui-tab__bd-item--active');
        $this->assign('tabbnt1', 'weui-bar__item--on');
        $this->assign('result', $result);
        $this->assign('presult', $presult);
        $this->display('index');

    }

    function ok(){
        if(session('showseq') == ''){
            session('[destroy]');
            session('[regenerate]');
            $this->display('index');
        }else{
            $this->assign('data', session('showseq'));
            $this->display('ok');
        }

    }

    function p($arr){
        dump($arr, 1, '<pre>', 0);
    }

    function getmoney($num){
        $money = 0;

        if($num==2){
            $money = 280;
        }elseif ($num){
            $money = 220;
        }elseif ($num >=3 && $num <=5){
            $money = 180;
        }elseif ($num >= 6){
            $money = 150;
        }else{
            $money = -1;
        }

        return $money;
    }

}